package com.touchalachs.game;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.view.SurfaceHolder;

import com.touchalachs.core.GameView;
import com.touchalachs.core.ScrollingBackground;
import com.touchalachs.core.Updatable;
import com.touchalachs.game.Res.ResThreadVisibilityEnsurer;

public class LachsGameView extends GameView implements Updatable {

	private static final String COOKIE = "cookie";
	private static Calculator calculator;

	public LachsGameView(Context context){
		this(context, new Point (1,1));
	}
	public LachsGameView(Context context, Point screnSize) {
		super(context, screnSize);
	}

	@Override
	protected void initObjects(Point screenSize) {
		Res.init(getResources());
		SharedPreferences sharedPref = ((Activity) getContext()).getPreferences(Context.MODE_PRIVATE);
		String calculatorSave = sharedPref.getString(COOKIE, "");
		
		calculator = new Calculator(calculatorSave);
		ScrollingBackground background = new ScrollingBackground(screenSize, Res.WATER, 30);
		Score score = new Score(screenSize, calculator);
		Lachs lachs = new Lachs(40, 40, 20, 20, screenSize, calculator);
		WorkerInfoPopup workerPopup = new WorkerInfoPopup(screenSize, calculator);
		StatsPopup statsPopup = new StatsPopup(screenSize, calculator);
		BottomPanel worker = new BottomPanel(screenSize, calculator, workerPopup, statsPopup);

		addUpdatable(new ResThreadVisibilityEnsurer());
		addUpdatable(background);
		addUpdatable(calculator);
		addUpdatable(lachs);
		
		addDrawable(background);
		addDrawable(lachs);
		addDrawable(worker);
		addDrawable(score);
		addDrawable(workerPopup);
		addDrawable(statsPopup);
		
		addTouchable(workerPopup);
		addTouchable(statsPopup);
		addTouchable(worker);
		addTouchable(lachs);
	}

	@Override
	protected void initUI(Point screnSize) {	
	}
	
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		super.surfaceDestroyed(holder);
		SharedPreferences sharedPref = ((Activity) getContext()).getPreferences(Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPref.edit();
		editor.putString(COOKIE, calculator.getSaveString());
		editor.commit();
	}
}
